package com.zsummer.zero.jdk.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient {

	public static void main(String[] args) {
		String host = "127.0.0.1";
		int port = 55533;
		Socket socket = null;
		OutputStream os = null;
		InputStream is = null;
		try {
			socket = new Socket(host, port);
			os = socket.getOutputStream();
			os.write("dear i com, wash clean".getBytes("UTF-8"));
			socket.shutdownOutput();
			
			is = socket.getInputStream();
			byte[] bytes = new byte[1024];
			int len = 0;
			StringBuilder sb = new StringBuilder();
			while((len = is.read(bytes)) != -1) {
				sb.append(new String(bytes, 0, len, "UTF-8"));
			}
			System.out.println(sb.toString());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
				if (os != null) {
					os.close();
				}
				if (socket != null) {
					socket.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
